/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rentcalculator.persist;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import rentcalculator.controller.BillCalculator;
import rentcalculator.controller.BillCalculatorImpl;
import rentcalculator.model.Bill;
import rentcalculator.model.Charge;
import rentcalculator.model.Facility;
import rentcalculator.model.Lodgement;
import rentcalculator.model.Person;
import rentcalculator.model.Property;
import rentcalculator.util.DateUtil;
import test.Family;
import test.User;

/**
 *
 * @author K.E.Y
 */
public class mainTester {
        
        static Configuration cfg = new Configuration();
         
         
         
         
       public static void main(String args[] )throws Exception{
            //logicTest();
            
            //techTest();
           //testPropertyPersist();
            
//           
//           prepareRenterData();
//           
//           mainTest();
//           
//           
//           SessionController.getInstance().close();
           
           DecimalFormat df = new DecimalFormat("###,###,###.##");
           System.out.println(df.format(1231213.125441));
       }
       
        
       
       private static void mainTest(){
           
           Bill b = new BillDao().findById(1);
           BillCalculator bc = new BillCalculatorImpl();
           List<Charge> lcs = bc.compute(b,null);
           
           System.out.println(lcs.size());
           
           
           
           
           
       }
       
       private static void prepareRenterData() throws Exception{
           PersonDao personDao = new PersonDao();
           List<Person> personList = new ArrayList<>();
           Person seven = new Person("7");
           Person boy = new Person("boy");
           Person me = new Person("me");
           Person bb = new Person("bb");
           
           personList.add(seven);
           personList.add(boy);
           personList.add(me);
           personList.add(bb);
           
           for(Person p : personList){
               personDao.add(p);
           }
           
           PropertyDao propertyDao = new PropertyDao();
           Property property = new Property();
           propertyDao.add(property);
           
           FacilityDao facilityDao = new FacilityDao();
           List<Facility> lfs = new ArrayList<>();
           Facility water = new Facility("water", property);
           Facility gas   = new Facility("gas", property);
           Facility elec  = new Facility("elec", property);
           Facility net  = new Facility("net", property);
           Facility roomSeven = new Facility("roomSeven", property);
           Facility roomBoy = new Facility("roomBoy", property);
           Facility middleRoom = new Facility("middleRoom", property);
           Facility mainBedroom = new Facility("mainBedroom", property);

           
           lfs.add(water);
           lfs.add(gas);
           lfs.add(elec);
           lfs.add(net);
           lfs.add(roomSeven);
           lfs.add(roomBoy);
           lfs.add(middleRoom);
           lfs.add(mainBedroom);
           
           for(Facility f: lfs){
               facilityDao.add(f);
           }
           
           LodgementDao renterDao = new LodgementDao();
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-23"), boy, roomBoy));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-23"), boy, water));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-23"), boy, gas));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-23"), boy, elec));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-23"), boy, net));
           
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-01"), seven, roomSeven));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-01"), seven, water));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-01"), seven, gas));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-01"), seven, elec));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-01"), seven, net));
           
           renterDao.add(new Lodgement(DateUtil.genDate("2010-10-01"), me, middleRoom));
           renterDao.add(new Lodgement(DateUtil.genDate("2010-10-01"), me, water));
           renterDao.add(new Lodgement(DateUtil.genDate("2010-10-01"), me, gas));
           renterDao.add(new Lodgement(DateUtil.genDate("2010-10-01"), me, elec));
           renterDao.add(new Lodgement(DateUtil.genDate("2010-10-01"), me, net));
           
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-15"), bb, mainBedroom));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-15"), bb, water));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-15"), bb, gas));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-15"), bb, elec));
           renterDao.add(new Lodgement(DateUtil.genDate("2014-02-15"), bb, net));
           
           Bill n1 = new Bill("network-May", DateUtil.genDate("2014-05-01"),DateUtil.genDate("2014-05-31"),
                   net, 119.95, "AUD");
           BillDao billDao = new BillDao();
           
           
           
           
           
           billDao.add(n1);
           
           
       }
       
       private static void renterTest() throws Exception{
           PersonDao pd = new PersonDao();
           pd.add(new Person("P 1"));
           
           PropertyDao propertyDao = new PropertyDao();
           propertyDao.add(new Property());
           
            LodgementDao ld = new LodgementDao();
            List l = ld.findRentersByFacilityId(1, 
                    new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2013-01-01").getTime())
                    , new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2013-04-01").getTime()));
            
            
            
            SessionController.getInstance().close();
            System.out.println(l.size());
           
            
            Lodgement renter = (Lodgement)(l.get(0));
            System.out.println(renter.getPerson().getId());
       }
       
       private static void logicTest(){
           cfg.configure("resources/hibernate.cfg.xml");
            ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
            SessionFactory sf = cfg.buildSessionFactory(serviceRegistry);
             
            Session session = sf.openSession();
            session.beginTransaction();
            Family f = addFamily(session);
            
  
            
            User u3 = new User();
            u3.setId(1);
            u3.setFamily(f);
            u3.setName("test update user");
            session.delete(u3);
            
            
            
            session.getTransaction().commit();
            session.close();
            sf.close();
       }
       
       private static void techTest() throws Exception{
           System.out.println(System.currentTimeMillis());
           
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           System.out.println(Math.ceil((sdf.parse("2014-01-02").getTime()-sdf.parse("2014-01-01").getTime())/24/1000/3600)+1);
           System.out.println(sdf.parse("2014-02-01"));
           
       }
       
       private static User addUser(Session s, Family f){
           User u1 = new User();
            
            u1.setName("User "+ (9999 * Math.random()));
            
            u1.setFamily(f);
            s.save(u1);
            return u1;
       }
       
       private static User deleteUser(Session s ){
           //1. find the user by id
           User u = (User)s.get(User.class, (long)1);
            
           //2. if found, then delete the user
           if(u != null){
               s.delete(u);
               s.save(u);
           }
            
            return u;
       }
       
       private static Family addFamily(Session s){
           Family f = new Family();
            
            s.save(f);
            return f;
       }
       
       private static void testPropertyPersist(){
           
           Session s = SessionController.getInstance().getSession();
           s.save(new Property());
           s.flush();
           s.close();
           
           List<Property> lps = Persist.getInstance().findALL(Property.class);
           Iterator<Property> iter = lps.iterator();
           while(iter.hasNext()){
               Property p = iter.next();
               
               System.out.println(p.toString());
           }
           Property p10 = (Property)Persist.getInstance().findById(Property.class, (long)1);
          
           if(p10 == null ){
               System.out.println("property 10 is not found");
           }else{
               System.out.println("property 10 exists");
           }
            p10.setName("test2");
            Persist.getInstance().update(p10);
           
           SessionController.getInstance().close();
       }
       
}
